package liangtWorkSpace.stream;

import java.util.*;

/**
 * @ClassName ReductionStream
 * @Description : 演示Stream归约
 * @Author : LiangT
 * @Date : 2020/4/27 18:58
 */
public class ReductionStream {

    public static void main(String[] args) {

        /**
         * 演示三种List初始化方式
         */
        List<Integer> numbers1 = new ArrayList<Integer>(){{
            add(new Integer(1));
            add(new Integer(2));
            add(new Integer(3));
            add(new Integer(4));
            add(new Integer(5));
            add(new Integer(6));
            add(new Integer(7));
            add(new Integer(8));
            add(new Integer(9));
        }};
        Integer[] number = {1,2,3,4,5,6,7,8,9};
        List<Integer> numbers2 = new ArrayList<Integer>( Arrays.asList(number));
        List<Integer> numbers3 = new ArrayList<>();
        for (Integer integer: number){
            numbers3.add(integer);
        }
        int sum2 = 100;
        //求和
        int sum1 = numbers1.stream().reduce(sum2,(a,b)->a+b);
        System.out.println("sum1求和结果："+sum1);
        //获取最大值
        Optional<Integer> max = numbers2.stream().reduce(Integer::max);
        Optional<Integer> maxLamdba = numbers2.stream().reduce((a,b)->a>b?a:b);
        //获取最小值
        Optional<Integer> min = numbers2.stream().reduce(Integer::min);
        Optional<Integer> minLamdba = numbers2.stream().reduce((a,b)->a>b?b:a);
        //获取数组数量
        int count = numbers2.stream().map(d->1).reduce(0,(a,b)->a+b);
        System.out.println(count);
    }
}

